part of my_base_lib;

class FileUtils {
  static Directory? appDocumentsDirectory; //系统提供的可用文件夹（用于当做存储根目录）
  static String? appDocumentsDirectoryPath; //系统提供的可用文件夹（用于当做存储根目录）

  static Directory? cacheDirectory; //用来做缓存的文件夹 //每次应用启动时清空
  static String? cacheDirectoryPath; //用来做缓存的文件夹

  static init() async {
    appDocumentsDirectory = await getApplicationDocumentsDirectory();
    appDocumentsDirectoryPath = "${appDocumentsDirectory!.path}/";
    initCacheDirectory();
  }

  //根据文件名 获取 默认的文件存放路径
  static String getDefaultPathByFileName(String fileName) {
    return "${FileUtils.appDocumentsDirectoryPath}$fileName";
  }

  //根据文件名 获取 缓存文件夹的文件存放路径
  static String getCachePathByFileName(String fileName) {
    return "${FileUtils.cacheDirectoryPath}$fileName";
  }

  static Directory getDirByName(
      {required String parentPath, required String dirName}) {
    if (!parentPath.endsWith("/")) {
      parentPath += "/";
    }
    if (!dirName.endsWith("/")) {
      dirName += "/";
    }
    return getDirByPath(path: parentPath + dirName);
  }

  static Directory getDirByPath({required String path}) {
    if (!path.startsWith(appDocumentsDirectoryPath!)) {
      path = '$appDocumentsDirectoryPath$path';
    }
    final directory = Directory(path);
    if (!directory.existsSync()) {
      return createDirByPath(path: path);
    }
    return directory;
  }

  static Directory createDirByPath({required String path}) {
    if (!path.startsWith(appDocumentsDirectoryPath!)) {
      path = '$appDocumentsDirectoryPath$path';
    }
    final directory = Directory(path);
    if (directory.existsSync()) {
      directory.deleteSync(recursive: true);
    }
    directory.createSync(recursive: true);
    return directory;
  }

  static deleteDir({required String path}) {
    final directory = Directory(path);
    if (directory.existsSync()) {
      directory.deleteSync(recursive: true);
    }
  }

  static File getFile({required String path}) {
    final file = File(path);
    if (!file.existsSync()) {
      createFile(path: path);
    }
    return file;
  }

  static File createFile({required String path}) {
    final file = File(path);
    if (file.existsSync()) {
      file.deleteSync(recursive: true);
    }
    file.createSync();
    return file;
  }

  static deleteFile({required String path}) {
    final file = File(path);
    if (file.existsSync()) {
      file.deleteSync(recursive: true);
    }
  }

  static String? readStringFormFile({required String path}) {
    final file = File(path);
    if (file.existsSync()) {
      return file.readAsStringSync();
    }
    return null;
  }

  static writeStringToFile(
      {required String string, required String path, bool append = false}) {
    final file = File(path);
    if (!file.existsSync()) {
      file.createSync(recursive: true);
    }
    file.writeAsStringSync(string,
        mode: append ? FileMode.append : FileMode.write);
  }

  static List<int>? readBytesFormFile({required String path}) {
    final file = File(path);
    if (file.existsSync()) {
      return file.readAsBytesSync();
    }
    return null;
  }

  static writeBytesToFile(
      {required List<int> bytes, required String path, bool append = false}) {
    final file = File(path);
    if (!file.existsSync()) {
      file.createSync(recursive: true);
    }
    file.writeAsBytesSync(bytes,
        mode: append ? FileMode.append : FileMode.write);
  }

  static void initCacheDirectory() {
    cacheDirectoryPath = ("${appDocumentsDirectoryPath}cacheDirectory/");
    cacheDirectory = createDirByPath(path: cacheDirectoryPath!);
  }
}
